﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;
using System.IO;
using HRM.Utilities;
using System.Runtime.InteropServices;

namespace HRM
{
    public partial class frmPremiumList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _employeeID = 0;
        int _PremiumID = 0;
        EmployeeClient _wcfEmployee;
        PremiumClient _wcfPremium;
        string _InsBookNo = "";
        RoleRightInfo listRole;
        bool _IsLoaded = false;
        DockPanel docPanel;


        public frmPremiumList()
        {
            InitializeComponent();

            

            _wcfEmployee = Service.Employee();
            _wcfPremium = Service.Premium();
            listRole = UIParamater.Quyen_PhatSinhBaoHiem;
            txtKeyWord.Focus();
            this.KeyPreview = true;
            toolStripSeparator3.Visible = false;
            btnPrint.Visible = false;
            BuildEmployeeGrid();
            AddColumnPremium();
            LoadEmployeeData();

            if (!listRole.Create)
            {
                btnNew.Enabled = false;
                btnExcel.Enabled = false;
            }
            if (!listRole.Update)
                btnEdit.Enabled = false;
            if (!listRole.Delete)
                btnDel.Enabled = false;

        }

        [DllImport("uxtheme.dll")]
        public static extern int SetWindowTheme(
        [In] IntPtr hwnd,
        [In, MarshalAs(UnmanagedType.LPWStr)] string pszSubAppName,
        [In, MarshalAs(UnmanagedType.LPWStr)] string pszSubIdList
        );

        /// <summary>
        /// Thêm mới quá trình trước đây
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                UISetting.MesNotRightNew();
                return;
            }
            if (dgvList.SelectedCells.Count > 0)
            {
                //Form f = new frmPremiumInfo(0, _employeeID, 0, listRole);
                Form f = new frmPremiumInfo(0, _employeeID, 0, listRole);
                ((frmPremiumInfo)f).AfterCreateEvent += new frmPremiumInfo.AfterCreateOrSaveItem(frmPremiumList_AfterCreateEvent);
                if ( (f as frmPremiumInfo).Show_Form() == DialogResult.OK)
                {
                    LoadPremium(_InsBookNo);
                }
            }
            else
                MessageBox.Show("Hãy chọn 1 nhân viên trong danh sách!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        private void BuildEmployeeGrid()
        {
            if (dgvList.ColumnCount <= 0)
            {
                UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã chính", "", 1, false, true);
                UISetting.AddColumn(ref dgvList, "RowNumber", UpdateUI.GetString("stt"), "C", 30, true, true);
                UISetting.AddColumn(ref dgvList, "Employee_Code", UpdateUI.GetString("maso"), "", 120, false, true);
                UISetting.AddColumn(ref dgvList, "Employee_FullName", UpdateUI.GetString("hovaten"), "", 130, true, true);
                //UISetting.AddColumn(ref dgvList, "Employee_CardNo", "", "C", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Employee_SIBookNo", "Số sổ BH", "L", 90, true, true);
                UISetting.AddColumn(ref dgvList, "Employee_Sex", "GT", "C", 40, true, true);
                UISetting.AddColumn(ref dgvList, "Employee_DateOfBirth", UpdateUI.GetString("ngaysinh"), "C", 80, true, true);
            }
        }
        private void LoadEmployeeData()
        {
            DataSet ds = _wcfEmployee.Employee_GetList(out itemCount, Program.intBranchID, 0, UISetting.GetKeyWord(txtKeyWord.Text.Trim()), "", "", currPage, pageSize);
            int n = ds.Tables[0].Rows.Count;

            //hiepdh: ko có thuoc don vi nay => lay qua trinh thuoc don vi khac
            //nếu ko có thuộc đơn vị này => lấy thông tin từ đơn vị khác theo số sổ
            if (n == 0)
            {
                ds = _wcfEmployee.Employee_GetList(out itemCount, 0, 0, UISetting.GetKeyWord(txtKeyWord.Text.Trim()), "", "", currPage, pageSize);
                n = ds.Tables[0].Rows.Count;
            }

            dgvList.Rows.Clear();
            PageIndex();
            for (int i = 0; i < n; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Columns["Employee_FullName"].Frozen = true;
                dgvList.Rows[i].Cells["RowNumber"].Value = ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvList.Rows[i].Cells["Employee_ID"].Value = ds.Tables[0].Rows[i]["Employee_ID"].ToString();
                dgvList.Rows[i].Cells["Employee_Code"].Value = ds.Tables[0].Rows[i]["Employee_Code"].ToString();
                //dgvList.Rows[i].Cells["Employee_CardNo"].Value = ds.Tables[0].Rows[i]["Employee_CardNo"].ToString();
                dgvList.Rows[i].Cells["Employee_FullName"].Value = ds.Tables[0].Rows[i]["Employee_FullName"].ToString();
                dgvList.Rows[i].Cells["Employee_SIBookNo"].Value = ds.Tables[0].Rows[i]["Employee_SIBookNo"].ToString();
                if (ds.Tables[0].Rows[i]["Employee_DateOfBirth"] != DBNull.Value)
                    dgvList.Rows[i].Cells["Employee_DateOfBirth"].Value = Convert.ToDateTime(ds.Tables[0].Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
                else
                    dgvList.Rows[i].Cells["Employee_DateOfBirth"].Value = "";
                dgvList.Rows[i].Cells["Employee_Sex"].Value = ds.Tables[0].Rows[i]["Employee_Sex"].ToString();
            }
            _IsLoaded = true;
        }

        private void AddColumnPremium()
        {
            UISetting.AddColumn(ref dgvPremium, "Premium_Date", "Ngày nhập", "C", 100, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_ID", "Mã chính", "", 1, false, true);
            //UISetting.AddColumn(ref dgvPremium, "RowNumber", UpdateUI.GetString("stt"), "C", 30, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_FromDate", "Từ tháng", "C", 80, true, true).DefaultCellStyle.Format="MM/yyyy";
            UISetting.AddColumn(ref dgvPremium, "Premium_ToDate", "Đến tháng", "C", 90, true, true).DefaultCellStyle.Format = "MM/yyyy";
            //UISetting.AddColumn(ref dgvPremium, "Premium_ToDate_Chuan", "Đến tháng", "C", 90, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_WorkUnit", UpdateUI.GetString("donvilamviec"), "", 150, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_Job", UpdateUI.GetString("nghenghiep"), "", 100, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_Salary", "ML", "R", 70, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_BasicFactor", "HSL", "R", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_PositionFactor", "HS Cvụ", "R", 90, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_SeniorityFactorPercent", "% TNNghề", "R", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_SeniorityFactor", "HS TNNghề", "R", 60, true, true);
            
            UISetting.AddColumn(ref dgvPremium, "Premium_OverSeniorityFactorPercent", "% TNVK", "R", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_OverSeniorityFactor", "HS TNVK", "R", 60, true, true);

            UISetting.AddColumn(ref dgvPremium, "Premium_AreaFactor", "HS KV", "R", 100, false, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_MonthNo", "Số tháng", "R", 100, true, true);

            UISetting.AddColumn(ref dgvPremium, "Premium_MonthNo", "Số tháng", "R", 100, true, true);
            UISetting.AddColumn(ref dgvPremium, "PA", "PA", "R", 60, true, true);

            UISetting.AddColumn(ref dgvPremium, "Premium_Description", UpdateUI.GetString("ghichu"), "", 100, false, true);
            UISetting.AddColumn(ref dgvPremium, "Detail_ID", "Detail_ID", "", 30, false, true);
        }
        private void LoadPremium(string _SIBookNo)
        {
            try
            {
                DataTable dt = new DataTable();
                PremiumClient _wcfPremium = Service.Premium();
                //dt = _wcfPremium.Premium_QTBH(Program.strBranchCode, _SIBookNo, 0, DateTime.MinValue, DateTime.MaxValue).Tables[0];
                dt = _wcfPremium.Premium_GetBySIBookNo(_SIBookNo, false).Tables[0];
                dgvPremium.Rows.Clear();
                dt.DefaultView.Sort = "Premium_FromDate ASC";
                dt = dt.DefaultView.ToTable();
                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dgvPremium.Rows.Insert(i, 1);
                        //dgvList.Columns["Premium_ToDate"].Frozen = true;
                        if (Convert.ToDateTime(dt.Rows[i]["Premium_Date"]).ToString("dd/MM/yyyy") != "01/01/2000")
                            dgvPremium.Rows[i].Cells["Premium_Date"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_Date"]).ToString("dd/MM/yyyy");    
                        dgvPremium.Rows[i].Cells["Premium_ID"].Value = dt.Rows[i]["Premium_ID"].ToString();
                        //dgvPremium.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                        dgvPremium.Rows[i].Cells["Premium_FromDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_FromDate"]);
                        //--dgvPremium.Rows[i].Cells["Premium_ToDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_ToDate"]).ToString("MM/yyyy");
                        dgvPremium.Rows[i].Cells["Premium_ToDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_ToDate"]);
                        dgvPremium.Rows[i].Cells["Premium_WorkUnit"].Value = dt.Rows[i]["Premium_WorkUnit"].ToString();
                        dgvPremium.Rows[i].Cells["Premium_Job"].Value = dt.Rows[i]["Premium_Job"].ToString();
                        dgvPremium.Rows[i].Cells["Premium_Salary"].Value = dt.Rows[i]["Premium_Salary"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_Salary"]).ToString("#,##0");
                        dgvPremium.Rows[i].Cells["Premium_BasicFactor"].Value = dt.Rows[i]["Premium_BasicFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_BasicFactor"]).ToString("#0.0##");
                        dgvPremium.Rows[i].Cells["Premium_PositionFactor"].Value = dt.Rows[i]["Premium_PositionFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_PositionFactor"]).ToString("#0.0##");
                        dgvPremium.Rows[i].Cells["Premium_SeniorityFactor"].Value = dt.Rows[i]["Premium_SeniorityFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_SeniorityFactor"]).ToString("#0.0##");

                        dgvPremium.Rows[i].Cells["Premium_OverSeniorityFactor"].Value = dt.Rows[i]["Premium_OverSeniorityFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_OverSeniorityFactor"]).ToString("#0.0##");
                        dgvPremium.Rows[i].Cells["Premium_AreaFactor"].Value = dt.Rows[i]["Premium_AreaFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_AreaFactor"]).ToString("#0.0##");
                        dgvPremium.Rows[i].Cells["Premium_MonthNo"].Value = dt.Rows[i]["Premium_MonthNo"].ToString();

                        string PA = dt.Rows[i]["Detail_InsTypeCode"].ToString();
                        PA = "ON,TS,OF".IndexOf(PA) > 0 ? PA : "";
                        dgvPremium.Rows[i].Cells["PA"].Value = PA;

                        dgvPremium.Rows[i].Cells["Premium_Description"].Value = dt.Rows[i]["Premium_Description"].ToString();
                        dgvPremium.Rows[i].Cells["Detail_ID"].Value = dt.Rows[i]["Detail_ID"].ToString();

                        //Tính % nghề, vk

                        decimal BasicFactor = UISetting.GetDecimalNo(dgvPremium.Rows[i].Cells["Premium_BasicFactor"].Value.ToString());
                        decimal PosionFactor = UISetting.GetDecimalNo(dgvPremium.Rows[i].Cells["Premium_PositionFactor"].Value.ToString());
                        decimal OverFactor = UISetting.GetDecimalNo(dgvPremium.Rows[i].Cells["Premium_OverSeniorityFactor"].Value.ToString());
                        decimal JobFactor = UISetting.GetDecimalNo(dgvPremium.Rows[i].Cells["Premium_SeniorityFactor"].Value.ToString());
                        //
                        if (OverFactor != 0)
                        {
                            decimal OverFactor_Percent = (OverFactor * 100 / BasicFactor);
                            dgvPremium.Rows[i].Cells["Premium_OverSeniorityFactorPercent"].Value = Math.Round(OverFactor_Percent).ToString();
                        }
                        if (JobFactor != 0)
                        {
                            decimal JobFactor_Percnet = (JobFactor * 100 / (BasicFactor + PosionFactor + OverFactor));
                            dgvPremium.Rows[i].Cells["Premium_SeniorityFactorPercent"].Value = Math.Round(JobFactor_Percnet).ToString();
                        }
                    }

                    //duyệt lại tìm dòng không tham gia, đảm bảo đã order theo từ tháng
                    int counter = 0;
                    DateTime FromDate, ToDate, NextFromDate;
                    for (int i = 1; i < dt.Rows.Count; i++)
                    {
                        ToDate = (DateTime)dt.Rows[i - 1]["Premium_ToDate"];
                        FromDate = (DateTime)dt.Rows[i]["Premium_FromDate"];

                        //if(
                        //NextFromDate = (DateTime)dgvPremium.Rows[i+1].Cells["Premium_FromDate"].Value;
                        //
                        if (ToDate.AddMonths(1) != FromDate)
                        {
                            //ko tham gia
                            dgvPremium.Rows.Insert(i + counter, 1);
                            dgvPremium.Rows[i + counter].Cells["Premium_FromDate"].Value = ToDate.AddMonths(1);
                            dgvPremium.Rows[i + counter].Cells["Premium_ToDate"].Value = FromDate.AddMonths(-1);
                            dgvPremium.Rows[i + counter].Cells["Premium_WorkUnit"].Value = "Không tham gia";
                            //
                            dgvPremium.Rows[i + counter].DefaultCellStyle.BackColor = Color.Yellow;
                            dgvPremium.Rows[i + counter].ReadOnly = true;
                            counter += 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvPremium.SelectedCells.Count > 0)
            {
                int _DetailID = 0;
                if (listRole.Update || listRole.View)
                {
                    _PremiumID = Convert.ToInt32(dgvPremium.Rows[dgvPremium.SelectedCells[0].RowIndex].Cells["Premium_ID"].Value);
                    _DetailID = Convert.ToInt32(dgvPremium.Rows[dgvPremium.SelectedCells[0].RowIndex].Cells["Detail_ID"].Value);
                    Form f = new frmPremiumInfo(_PremiumID, _employeeID, _DetailID, listRole);
                    ((frmPremiumInfo)f).AfterCreateEvent += new frmPremiumInfo.AfterCreateOrSaveItem(frmPremiumList_AfterCreateEvent);
                    if (((frmPremiumInfo)f).Show_Form() == DialogResult.OK)
                    {
                        LoadPremium(_InsBookNo);
                    }
                }
                else
                    UISetting.MesNotRightView();

            }
        }

        void frmPremiumList_AfterCreateEvent()
        {
            LoadPremium(_InsBookNo);
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            _IsLoaded = false;
            LoadEmployeeData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
            {
                currPage++;
            }
            _IsLoaded = false;
            LoadEmployeeData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            _IsLoaded = false;
            LoadEmployeeData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage > 0)
                currPage = totalPage - 1;
            _IsLoaded = false;
            LoadEmployeeData();
        }

        private void dgvPremiumList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmPremiumList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.N:
                    if (e.Control)
                        if (btnNew.Enabled)
                            btnNew_Click(null, null);
                    break;

                case Keys.Delete:
                    if (e.Control)
                        if (btnDel.Enabled)
                            btnDel_Click(null, null);
                    break;
                case Keys.E:
                    if (e.Control)
                        if (btnEdit.Enabled)
                            btnEdit_Click(null, null);
                    break;
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
                case Keys.F2:
                    btnExcel_Click(null, null);
                    break;
                case Keys.F7:
                    //btnList_Click(null, null);
                    break;
                //case Keys.Enter:
                //    if (!e.Alt && !e.Control)
                //    {
                //        this.ProcessTabKey(!e.Shift);
                //        e.Handled = true;
                //    }
                //    break;
            }
        }

        private void frmInsuranceList_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfEmployee.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvPremium.SelectedCells.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _PremiumID = Convert.ToInt32(dgvPremium.Rows[dgvPremium.SelectedCells[0].RowIndex].Cells["Premium_ID"].Value);
                //if (_wcfPremium.Premium_Delete(_PremiumID))
                //{
                //    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    return;
                //}
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfPremium.Premium_Delete(_PremiumID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadPremium(_InsBookNo);
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvList.SelectedCells.Count > 0)
            {
                _employeeID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                _InsBookNo = dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_SIBookNo"].Value.ToString();
                LoadPremium(_InsBookNo);
            }
        }

        private void dgvList_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvList.SelectedCells.Count > 0)
            {
                //Load quá trình BH trước đây
                if (_IsLoaded)
                {
                    _employeeID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                    _InsBookNo = dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_SIBookNo"].Value.ToString();
                    LoadPremium(_InsBookNo);
                }

                //Load quá trình BHXH
                _TabQTBHXH_IsLoaded = false;
                _TabQTBHTN_IsLoaded = false;
                //Nếu đang mở tab Quá trình BHXH thì load dữ liệu trên tab, ko thì đợi khi nào selected changed đến tab này thì load
                tabControl1_SelectedIndexChanged(null, null);
                
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            currPage = 0;
            _IsLoaded = false;
            LoadEmployeeData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    btnSearch_Click(null, null);
                    break;
            }
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            frmTime f = new frmTime(1);
            string fileName = "QTBHXH.xls";
            
            DataTable dt = new DataTable();

            if (f.ShowDialog(this) == DialogResult.OK)
            {
                //f.FromDate = f.FromDate == DateTime.MinValue Null.MIN_DATE

                dt = _wcfPremium.Premium_GetByDate((DateTime)Null.GetNull(f.FromDate, Null.MIN_DATE), (DateTime)Null.GetNull(f.ToDate, DateTime.MaxValue), Program.strBranchCode, 1, "").Tables[0];
                try
                {
                    if (dt.Rows.Count > 0)
                    {
                        ExportData2Excel(dt, Path.GetTempPath() + "\\" + fileName);
                        
                        try
                        {
                            System.Diagnostics.Process.Start(Path.GetTempPath() + "\\" + fileName);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Không mở được File \n" + Path.GetTempPath() + "\\" + fileName, "Thông báo");
                        }
                    }
                    else
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), "Không có dữ liệu!", UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File " + Path.GetTempPath() + "\\" + fileName + " đang được mở bằng một ứng dụng khác.\nHãy chọn file khác!", "Thông báo");
                }
            }
        }
        //private void ExportData2Excel(DataTable dt, string _FileName)
        //{
        //    DataTable dtEx = new DataTable();
        //    if (dt.Columns.Contains("RowNumber"))
        //        dt.Columns["RowNumber"].Caption = "STT";

        //    if (dt.Columns.Contains("Premium_SIBookNo"))
        //        dt.Columns["Premium_SIBookNo"].Caption = "SOBHXH";

        //    if (dt.Columns.Contains("Employee_FullName"))
        //        dt.Columns["Employee_FullName"].Caption = "HOTEN";

        //    if (dt.Columns.Contains("Premium_WorkUnit"))
        //        dt.Columns["Premium_WorkUnit"].Caption = "MADVI";

        //    if (dt.Columns.Contains("Premium_WorkName"))
        //        dt.Columns["Premium_WorkName"].Caption = "TENDVI";

        //    if (dt.Columns.Contains("Premium_Job"))
        //        dt.Columns["Premium_Job"].Caption = "NGHENGHIEP";

        //    if (dt.Columns.Contains("MaNTe"))
        //        dt.Columns["MaNTe"].Caption = "MA_NTE";

        //    if (dt.Columns.Contains("Premium_FromDate"))
        //        dt.Columns["Premium_FromDate"].Caption = "TUTHANG";

        //    if (dt.Columns.Contains("Premium_ToDate"))
        //        dt.Columns["Premium_ToDate"].Caption = "DENTHANG";

        //    if (dt.Columns.Contains("Premium_Salary"))
        //        dt.Columns["Premium_Salary"].Caption = "ML";

        //    if (dt.Columns.Contains("Premium_BasicFactor"))
        //        dt.Columns["Premium_BasicFactor"].Caption = "HSL";

        //    if (dt.Columns.Contains("Premium_PositionFactor"))
        //        dt.Columns["Premium_PositionFactor"].Caption = "PC1";

        //    if (dt.Columns.Contains("Premium_OverSeniorityFactor"))
        //        dt.Columns["Premium_OverSeniorityFactor"].Caption = "PC2";

        //    if (dt.Columns.Contains("Premium_SeniorityFactor"))
        //        dt.Columns["Premium_SeniorityFactor"].Caption = "PC3";

        //    if (dt.Columns.Contains("Premium_AreaFactor"))
        //        dt.Columns["Premium_AreaFactor"].Caption = "PC4";

        //    if (dt.Columns.Contains("Premium_InsTypeCode"))
        //        dt.Columns["Premium_InsTypeCode"].Caption = "PA";

        //    //Chuyển sang font TCVN3
        //    foreach (DataColumn dc in dt.Columns)
        //    {
        //        dc.ColumnName = UnicodeConvert.UnicodeToTCVN3(dc.ColumnName);
        //    }
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        foreach (DataColumn dc in dt.Columns)
        //            dr[dc] = UnicodeConvert.UnicodeToTCVN3(dr[dc].ToString());
        //    }

        //    //Theo mau 02
        //    //Xây dựng các cột trước
        //    //Xây dựng các cột trước
        //    //for (int i = 0; i < dt.Columns.Count; i++)
        //    //{
        //    //    dtEx.Columns.Add(dt.Columns[i].ColumnName, typeof(string));
        //    //}
        //    ////Gán dữ liệu vào các dòng tương ứng theo cột
        //    //DataRow dr;
        //    //for (int j = 0; j < dt.Rows.Count; j++)
        //    //{
        //    //    dr = dtEx.NewRow();
        //    //    //Liệt kê các cột theo thứ tự côt của bảng gốc, kết hợp gán dữ liệu dòng tương ứng cột
        //    //    for (int k = 0; k < dt.Columns.Count; k++)
        //    //    {
        //    //        dr[k] = UnicodeConvert.UnicodeToTCVN3(dt.Rows[j][k].ToString());
        //    //    }
        //    //    dtEx.Rows.InsertAt(dr, j);
        //    //}

        //    //

        //    ReadExcelFile.exportDataToExcel(dt, _FileName);
        //}
        private void ExportData2Excel(DataTable dt, string _FileName)
        {
            DataTable dtEx = new DataTable();
            if (dt.Columns.Contains("RowNumber"))
                dt.Columns["RowNumber"].Caption = "STT";

            if (dt.Columns.Contains("Premium_SIBookNo"))
                dt.Columns["Premium_SIBookNo"].Caption = "SOBHXH";

            if (dt.Columns.Contains("Employee_FullName"))
                dt.Columns["Employee_FullName"].Caption = "HOTEN";

            if (dt.Columns.Contains("Premium_WorkUnit"))
                dt.Columns["Premium_WorkUnit"].Caption = "MADVI";

            if (dt.Columns.Contains("Premium_WorkName"))
                dt.Columns["Premium_WorkName"].Caption = "TENDVI";          
            
            if (dt.Columns.Contains("Premium_Job"))
                dt.Columns["Premium_Job"].Caption = "NGHENGHIEP";

            if (dt.Columns.Contains("MaNTe"))
                dt.Columns["MaNTe"].Caption = "MA_NTE";

            if (dt.Columns.Contains("Premium_FromDate"))
                dt.Columns["Premium_FromDate"].Caption = "TUTHANG";

            if (dt.Columns.Contains("Premium_ToDate"))
                dt.Columns["Premium_ToDate"].Caption = "DENTHANG";

            //if (dt.Columns.Contains("Premium_ToDate_Chuan"))
            //    dt.Columns["Premium_ToDate_Chuan"].Caption = "DENTHANG";

            if (dt.Columns.Contains("Premium_Salary"))
                dt.Columns["Premium_Salary"].Caption = "ML";

            if (dt.Columns.Contains("Premium_BasicFactor"))
                dt.Columns["Premium_BasicFactor"].Caption = "HSL";

            if (dt.Columns.Contains("Premium_PositionFactor"))
                dt.Columns["Premium_PositionFactor"].Caption = "PC1";

            if (dt.Columns.Contains("Premium_OverSeniorityFactor"))
                dt.Columns["Premium_OverSeniorityFactor"].Caption = "PC2";

            if (dt.Columns.Contains("Premium_SeniorityFactor"))
                dt.Columns["Premium_SeniorityFactor"].Caption = "PC3";

            if (dt.Columns.Contains("Premium_AreaFactor"))
                dt.Columns["Premium_AreaFactor"].Caption = "PC4";

            //DataColumn dtCol = new DataColumn("PC5");
            //dtCol.Caption = "PC5";
            
            //dt.Columns["Premium_AreaFactor"].Caption = "PC5";
            
            if (dt.Columns.Contains("Premium_InsTypeCode"))
                dt.Columns["Premium_InsTypeCode"].Caption = "PA";

            //Theo mau 02
            //Xây dựng các cột trước
            //Xây dựng các cột trước
            DataColumn column;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                column = new DataColumn(dt.Columns[i].ColumnName,typeof(string));
                column.Caption = dt.Columns[i].Caption;
                dtEx.Columns.Add(column);
            }
            //Gán dữ liệu vào các dòng tương ứng theo cột
            DataRow dr;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                dr = dtEx.NewRow();
                //Liệt kê các cột theo thứ tự côt của bảng gốc, kết hợp gán dữ liệu dòng tương ứng cột
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    if (dt.Columns.Contains("Employee_FullName"))
                        dr[k] = UnicodeConvert.UnicodeToTCVN3(dt.Rows[j][k].ToString());
                    else
                        dr[k] = dt.Rows[j][k];
                }
                dtEx.Rows.InsertAt(dr, j);
            }

            //

            ReadExcelFile.exportDataToExcel(dtEx, _FileName);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //frmTime frm = new frmTime(2);
            //if (frm.ShowDialog(this) == DialogResult.OK)
            //{
            //    frmPreviewReport f = new frmPreviewReport("InQTBHXH", new string[] { _employeeID.ToString(), frm.FromDate.ToString("dd/MM/yyyy"), frm.ToDate.ToString("dd/MM/yyyy") });
            //    f.Show(this);
            //}
            if (dgvList.SelectedCells[0] != null)
            {
                if (listRole.Update || listRole.View)
                {
                    _employeeID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                    string SoBHXH = dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["EMPLOYEE_SIBOOKNO"].Value.ToString();

                    //frmInsurancePrint frm = new frmInsurancePrint();
                    //if (frm.ShowDialog(this) == DialogResult.OK)
                    //{
                    //frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH", new string[] { employeeID.ToString(), DateTime.Now.Date.ToString("dd/MM/yyyy"), DateTime.Now.Date.ToString("dd/MM/yyyy") });
                    frmPreviewReport f = new frmPreviewReport("BHXHMau07", new string[] { _employeeID.ToString(), null, null, SoBHXH });
                    f.Show(this);
                    //}
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //private void btnList_Click(object sender, EventArgs e)
        //{
        //    if (_employeeID > 0)
        //    {
        //        //Cu, show dialog
        //        SalaryFundClient _wcfSalaryFund = Service.SalaryFund();
        //        DataTable dtFund = _wcfSalaryFund.SalaryFund_GetDate(Program.strBranchCode).Tables[0];
        //        DateTime _DateV = DateTime.MinValue;
        //        if (dtFund.Rows.Count > 0)
        //            _DateV = Convert.ToDateTime(dtFund.Rows[0]["SALARYFUND_MONTH"].ToString());
        //        else
        //            _DateV = DateTime.Now.Date;
        //        frmQuaTrinhBHXH f = new frmQuaTrinhBHXH(_employeeID, _InsBookNo, _DateV, 0);
        //        f.ShowDialog();
        //    }
        //}

        //private void btnList_BHTN_Click(object sender, EventArgs e)
        //{
        //    if (_employeeID > 0)
        //    {
        //        SalaryFundClient _wcfSalaryFund = Service.SalaryFund();
        //        DataTable dtFund = _wcfSalaryFund.SalaryFund_GetDate(Program.strBranchCode).Tables[0];
        //        DateTime _DateV = DateTime.MinValue;
        //        if (dtFund.Rows.Count > 0)
        //            _DateV = Convert.ToDateTime(dtFund.Rows[0]["SALARYFUND_MONTH"].ToString());
        //        else
        //            _DateV = DateTime.Now.Date;
        //        frmQuaTrinhBHXH f = new frmQuaTrinhBHXH(_employeeID, _InsBookNo, _DateV,1);
        //        f.ShowDialog(this);
        //    }
        //}


//-------------------------------Tab quá trình bhxh-------------------------------------------------------------------------------------
        int _opt = 0;
        bool _TabQTBHXH_IsLoaded = false;
        DateTime _ViewDate = DateTime.MinValue;

        public void LoadfrmQuaTrinhBHXH(int EmployeeID, string InsBookNo, DateTime _DateVal, int opt)
        {
            _wcfEmployee = Service.Employee();
            _wcfPremium = Service.Premium();
            listRole = UIParamater.Quyen_PhatSinhBaoHiem;
            this.KeyPreview = true;
            _ViewDate = _DateVal;
            //_employeeID = EmployeeID;
            //_InsBookNo = InsBookNo;
            _opt = opt;
            TabQTBHXH_AddColumnPremium();
            TabQTBHXH_LoadPremium(_InsBookNo);
            _TabQTBHXH_IsLoaded = true;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //In quá trình BHXH
            //frmTime frm = new frmTime(2);
            //if (frm.ShowDialog(this) == DialogResult.OK)
            //{
            //    frmPreviewReport f = new frmPreviewReport("InQTBHXH", new string[] { _employeeID.ToString(), frm.FromDate.ToString("dd/MM/yyyy"), frm.ToDate.ToString("dd/MM/yyyy") });
            //    f.Show(this);
            //}
            if (dgvList.SelectedCells[0] != null)
            {
                if (listRole.Update || listRole.View)
                {
                    _employeeID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                    string SoBHXH = dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["EMPLOYEE_SIBOOKNO"].Value.ToString();

                    //frmInsurancePrint frm = new frmInsurancePrint();
                    //if (frm.ShowDialog(this) == DialogResult.OK)
                    //{
                    //frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH", new string[] { employeeID.ToString(), DateTime.Now.Date.ToString("dd/MM/yyyy"), DateTime.Now.Date.ToString("dd/MM/yyyy") });
                    frmPreviewReport f = new frmPreviewReport("BHXHMau07", new string[] { _employeeID.ToString(), null, null, SoBHXH });
                    f.Show(this);
                    //}
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void TabQTBHXH_AddColumnPremium()
        {
            if (dgvPremium_QTBHXH.ColumnCount <= 0)
            {
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_WorkUnit", UpdateUI.GetString("donvilamviec"), "", 150, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_FromDate", "Từ tháng", "C", 80, true, true);
                //--UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_ToDate", "Đến tháng", "C", 80, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_ToDate_Chuan", "Đến tháng", "C", 80, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "LoaiPs", "Loại", "C", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_Job", UpdateUI.GetString("nghenghiep"), "", 100, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_Salary", "ML", "R", 70, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_BasicFactor", "HSL", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_PositionFactor", "HS Cvụ", "R", 60, true, true);

                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_SeniorityFactorPercent", "% TNNghề", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_SeniorityFactor", "HS TNNghề", "R", 60, true, true);
                
                                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_OverSeniorityFactorPercent", "% TNVK", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_OverSeniorityFactor", "HS TNVK", "R", 60, true, true);

                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_AreaFactor", "HS KV", "R", 60, false, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_MonthNo", "Số tháng", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "PA", "PA", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHXH, "Premium_Description", UpdateUI.GetString("ghichu"), "", 100, false, true);
            }
        }

        private void TabQTBHXH_LoadPremium(string _SIBookNo)
        {
            try
            {
                DataTable dt = new DataTable();
                PremiumClient _wcfPremium = Service.Premium();
                dt = _wcfPremium.Premium_QTBH(Program.strBranchCode, _SIBookNo, _opt, DateTime.MinValue, DateTime.MaxValue,0).Tables[0];
                //dt = _wcfPremium.Premium_GetBySIBookNo(_SIBookNo, true).Tables[0];
                dgvPremium_QTBHXH.Rows.Clear();
                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dgvPremium_QTBHXH.Rows.Insert(i, 1);
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_WorkUnit"].Value = dt.Rows[i]["Premium_WorkUnit"].ToString();
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_FromDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_FromDate"]).ToString("MM/yyyy");
                        //--dgvPremium_QTBHXH.Rows[i].Cells["Premium_ToDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_ToDate"]).ToString("MM/yyyy");
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_ToDate_Chuan"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_ToDate_Chuan"]).ToString("MM/yyyy");
                        dgvPremium_QTBHXH.Rows[i].Cells["LoaiPs"].Value = dt.Rows[i]["LoaiPs"].ToString();
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_Job"].Value = dt.Rows[i]["Premium_Job"].ToString();
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_Salary"].Value = dt.Rows[i]["Premium_Salary"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_Salary"]).ToString("#,##0");
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_BasicFactor"].Value = dt.Rows[i]["Premium_BasicFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_BasicFactor"]).ToString("#0.0##");
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_PositionFactor"].Value = dt.Rows[i]["Premium_PositionFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_PositionFactor"]).ToString("#0.0##");

                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_SeniorityFactor"].Value = dt.Rows[i]["Premium_SeniorityFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_SeniorityFactor"]).ToString("#0.0##");
                        
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_OverSeniorityFactor"].Value = dt.Rows[i]["Premium_OverSeniorityFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_OverSeniorityFactor"]).ToString("#0.0##");
                        
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_AreaFactor"].Value = dt.Rows[i]["Premium_AreaFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_AreaFactor"]).ToString("#0.0##");
                        if (dt.Rows[i]["Premium_MonthNo"].ToString() != "0")
                            dgvPremium_QTBHXH.Rows[i].Cells["Premium_MonthNo"].Value = dt.Rows[i]["Premium_MonthNo"].ToString();
                        else
                            dgvPremium_QTBHXH.Rows[i].Cells["Premium_MonthNo"].Value = (UISetting.GetMonth(Convert.ToDateTime(dt.Rows[i]["Premium_FromDate"]), Convert.ToDateTime(dt.Rows[i]["Premium_ToDate_Chuan"])) + 1).ToString();

                        string PA = dt.Rows[i]["Detail_InsTypeCode"].ToString();
                        PA = "ON,TS,OF".IndexOf(PA) > 0 ? PA : "";
                        dgvPremium_QTBHXH.Rows[i].Cells["PA"].Value = PA;
                        dgvPremium_QTBHXH.Rows[i].Cells["Premium_Description"].Value = dt.Rows[i]["Premium_Description"].ToString();
                        if (dgvPremium_QTBHXH.Rows[i].Cells["Premium_WorkUnit"].Value.ToString() == "")
                        {
                            dgvPremium_QTBHXH.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                            dgvPremium_QTBHXH.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Yellow;
                        }

                        //Tính % nghề, vk

                        decimal BasicFactor = UISetting.GetDecimalNo(dgvPremium_QTBHXH.Rows[i].Cells["Premium_BasicFactor"].Value.ToString());
                        decimal PosionFactor = UISetting.GetDecimalNo(dgvPremium_QTBHXH.Rows[i].Cells["Premium_PositionFactor"].Value.ToString());
                        decimal OverFactor = UISetting.GetDecimalNo(dgvPremium_QTBHXH.Rows[i].Cells["Premium_OverSeniorityFactor"].Value.ToString());
                        decimal JobFactor = UISetting.GetDecimalNo(dgvPremium_QTBHXH.Rows[i].Cells["Premium_SeniorityFactor"].Value.ToString());
                        //
                        if (OverFactor != 0)
                        {
                            decimal OverFactor_Percent = (OverFactor * 100 / BasicFactor);
                            dgvPremium_QTBHXH.Rows[i].Cells["Premium_OverSeniorityFactorPercent"].Value = Math.Round(OverFactor_Percent).ToString();
                        }
                        if (JobFactor != 0)
                        {
                            decimal JobFactor_Percnet = (JobFactor * 100 / (BasicFactor + PosionFactor + OverFactor));
                            dgvPremium_QTBHXH.Rows[i].Cells["Premium_SeniorityFactorPercent"].Value = Math.Round(JobFactor_Percnet).ToString();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }


/*-------------------------------------Hiện dữ liệu trên tab Quá trình BHTN ----------------------------------------------------*/
        bool _TabQTBHTN_IsLoaded = false;

        public void LoadfrmQuaTrinhBHTN(int EmployeeID, string InsBookNo, DateTime _DateVal, int opt)
        {
            _wcfEmployee = Service.Employee();
            _wcfPremium = Service.Premium();
            listRole = UIParamater.Quyen_PhatSinhBaoHiem;
            this.KeyPreview = true;
            _ViewDate = _DateVal;
            //_employeeID = EmployeeID;
            //_InsBookNo = InsBookNo;
            _opt = opt;
            TabQTBHTN_AddColumnPremium();
            TabQTBHTN_LoadPremium(_InsBookNo);

            _TabQTBHTN_IsLoaded = true;
        }

        private void TabQTBHTN_AddColumnPremium()
        {
            if (dgvPremium_QTBHTN.ColumnCount <= 0)
            {
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_WorkUnit", UpdateUI.GetString("donvilamviec"), "", 150, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_FromDate", "Từ tháng", "C", 80, true, true);
                //--UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_ToDate", "Đến tháng", "C", 80, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_ToDate_Chuan", "Đến tháng", "C", 80, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "LoaiPs", "Loại", "C", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_Job", UpdateUI.GetString("nghenghiep"), "", 100, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_Salary", "ML", "R", 70, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_BasicFactor", "HSL", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_PositionFactor", "HS Cvụ", "R", 60, true, true);

                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_SeniorityFactorPercent", "% TNNghề", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_SeniorityFactor", "HS TNNghề", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_OverSeniorityFactorPercent", "% TNVK", "R", 60, true, true);

                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_OverSeniorityFactor", "HS TNVK", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_AreaFactor", "HS KV", "R", 60, false, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_MonthNo", "Số tháng", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "PA", "PA", "R", 60, true, true);
                UISetting.AddColumn(ref dgvPremium_QTBHTN, "Premium_Description", UpdateUI.GetString("ghichu"), "", 100, false, true);
            }
        }

        private void TabQTBHTN_LoadPremium(string _SIBookNo)
        {
            try
            {
                DataTable dt = new DataTable();
                PremiumClient _wcfPremium = Service.Premium();
                dt = _wcfPremium.Premium_QTBH(Program.strBranchCode, _SIBookNo, _opt, DateTime.MinValue, DateTime.MaxValue,0).Tables[0];
                //dt = _wcfPremium.Premium_GetBySIBookNo(_SIBookNo, true).Tables[0];
                dgvPremium_QTBHTN.Rows.Clear();
                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dgvPremium_QTBHTN.Rows.Insert(i, 1);
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_WorkUnit"].Value = dt.Rows[i]["Premium_WorkUnit"].ToString();
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_FromDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_FromDate"]).ToString("MM/yyyy");
                        //--dgvPremium_QTBHTN.Rows[i].Cells["Premium_ToDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_ToDate"]).ToString("MM/yyyy");
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_ToDate_Chuan"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_ToDate_Chuan"]).ToString("MM/yyyy");
                        dgvPremium_QTBHTN.Rows[i].Cells["LoaiPs"].Value = dt.Rows[i]["LoaiPs"].ToString();
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_Job"].Value = dt.Rows[i]["Premium_Job"].ToString();
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_Salary"].Value = dt.Rows[i]["Premium_Salary"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_Salary"]).ToString("#,##0");
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_BasicFactor"].Value = dt.Rows[i]["Premium_BasicFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_BasicFactor"]).ToString("#0.0##");
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_PositionFactor"].Value = dt.Rows[i]["Premium_PositionFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_PositionFactor"]).ToString("#0.0##");
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_SeniorityFactor"].Value = dt.Rows[i]["Premium_SeniorityFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_SeniorityFactor"]).ToString("#0.0##");
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_OverSeniorityFactor"].Value = dt.Rows[i]["Premium_OverSeniorityFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_OverSeniorityFactor"]).ToString("#0.0##");
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_AreaFactor"].Value = dt.Rows[i]["Premium_AreaFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_AreaFactor"]).ToString("#0.0##");
                        if (dt.Rows[i]["Premium_MonthNo"].ToString() != "0")
                            dgvPremium_QTBHTN.Rows[i].Cells["Premium_MonthNo"].Value = dt.Rows[i]["Premium_MonthNo"].ToString();
                        else
                            dgvPremium_QTBHTN.Rows[i].Cells["Premium_MonthNo"].Value = (UISetting.GetMonth(Convert.ToDateTime(dt.Rows[i]["Premium_FromDate"]), Convert.ToDateTime(dt.Rows[i]["Premium_ToDate"])) + 1).ToString();

                        string PA = dt.Rows[i]["Detail_InsTypeCode"].ToString();
                        PA = "ON,TS,OF".IndexOf(PA) > 0 ? PA : "";
                        dgvPremium_QTBHTN.Rows[i].Cells["PA"].Value = PA;
                        dgvPremium_QTBHTN.Rows[i].Cells["Premium_Description"].Value = dt.Rows[i]["Premium_Description"].ToString();
                        if (dgvPremium_QTBHTN.Rows[i].Cells["Premium_WorkUnit"].Value.ToString() == "")
                        {
                            dgvPremium_QTBHTN.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                            dgvPremium_QTBHTN.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Yellow;
                        }

                        //Tính % nghề, vk
                        
                        decimal BasicFactor = UISetting.GetDecimalNo(dgvPremium_QTBHTN.Rows[i].Cells["Premium_BasicFactor"].Value.ToString());
                        decimal PosionFactor = UISetting.GetDecimalNo(dgvPremium_QTBHTN.Rows[i].Cells["Premium_PositionFactor"].Value.ToString());
                        decimal OverFactor = UISetting.GetDecimalNo(dgvPremium_QTBHTN.Rows[i].Cells["Premium_OverSeniorityFactor"].Value.ToString());
                        decimal JobFactor = UISetting.GetDecimalNo(dgvPremium_QTBHTN.Rows[i].Cells["Premium_SeniorityFactor"].Value.ToString());
                        //
                        if (OverFactor != 0)
                        {
                            decimal OverFactor_Percent = (OverFactor * 100 / BasicFactor);
                            dgvPremium_QTBHTN.Rows[i].Cells["Premium_OverSeniorityFactorPercent"].Value = Math.Round(OverFactor_Percent).ToString();
                        }
                        if (JobFactor != 0)
                        {
                            decimal JobFactor_Percnet = (JobFactor * 100 / (BasicFactor + PosionFactor + OverFactor));
                            dgvPremium_QTBHTN.Rows[i].Cells["Premium_SeniorityFactorPercent"].Value = Math.Round(JobFactor_Percnet).ToString();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

               
/*----------------------------------------------------------------------------------------------------------------------------*/

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Tab QTBHXH
            if (tabControl1.SelectedTab.Name == "tabQuaTrinhBHXH" && !_TabQTBHXH_IsLoaded && _employeeID > 0)
            {
                SalaryFundClient _wcfSalaryFund = Service.SalaryFund();
                DataTable dtFund = _wcfSalaryFund.SalaryFund_GetDate(Program.strBranchCode).Tables[0];
                DateTime _DateV = DateTime.MinValue;
                if (dtFund.Rows.Count > 0)
                    _DateV = Convert.ToDateTime(dtFund.Rows[0]["SALARYFUND_MONTH"].ToString());
                else
                    _DateV = DateTime.Now.Date;
                
                _opt = 0;
                LoadfrmQuaTrinhBHXH(_employeeID, _InsBookNo, _DateV, _opt);
            }

            //TabQTBHTN
            if (tabControl1.SelectedTab.Name == "tabQuaTrinhBHTN" && !_TabQTBHTN_IsLoaded && _employeeID > 0)
            {
                SalaryFundClient _wcfSalaryFund = Service.SalaryFund();
                DataTable dtFund = _wcfSalaryFund.SalaryFund_GetDate(Program.strBranchCode).Tables[0];
                DateTime _DateV = DateTime.MinValue;
                if (dtFund.Rows.Count > 0)
                    _DateV = Convert.ToDateTime(dtFund.Rows[0]["SALARYFUND_MONTH"].ToString());
                else
                    _DateV = DateTime.Now.Date;

                _opt = 1;
                LoadfrmQuaTrinhBHTN(_employeeID, _InsBookNo, _DateV, _opt);
            }
        }

        private void frmPremiumList_Load(object sender, EventArgs e)
        {
            if (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1)
                SetWindowTheme(this.tabControl1.Handle, " ", " ");
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            _TabQTBHXH_IsLoaded = false;
            tabControl1_SelectedIndexChanged(null, null);
        }

        
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            _TabQTBHTN_IsLoaded = false;
            tabControl1_SelectedIndexChanged(null, null);
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedCells[0] != null)
            {
                if (listRole.Update || listRole.View)
                {
                    _employeeID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                    string SoBHXH = dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["EMPLOYEE_SIBOOKNO"].Value.ToString();

                    //frmInsurancePrint frm = new frmInsurancePrint();
                    //if (frm.ShowDialog(this) == DialogResult.OK)
                    //{
                    //frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH", new string[] { employeeID.ToString(), DateTime.Now.Date.ToString("dd/MM/yyyy"), DateTime.Now.Date.ToString("dd/MM/yyyy") });
                    frmPreviewReport f = new frmPreviewReport("BHXHMau07", new string[] { _employeeID.ToString(), null, null, SoBHXH });
                    f.Show(this);
                    //}
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
                      
    }
}
